package com.my_library_one.mybase;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.my_library_one.http.AnsynHttpRequest;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment implements AnsynHttpRequest.ObserverCallBack{

    protected View rootView;
    protected Activity mActivity;
    protected LayoutInflater mInflater;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = LayoutInflater.from(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(setLayoutId()>0){
            rootView = mInflater.inflate(setLayoutId(),null);
            ButterKnife.bind(this,rootView);
        }
        initView();
        initListener();
        initData();
        return rootView;
    }

    protected void initView(){}
    private void initListener(){}
    private void initData(){}

    /**
     * 设置布局layout
     * @return
     */
    protected abstract int setLayoutId();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }

    @Override
    public void onDestroyView() {
        OkHttpUtils.getInstance().cancelTag(this);
        super.onDestroyView();
    }

    /**
     * 接口请求成功
     * @param data     返回的数据 json
     * @param method   判断是哪个接口返回的数据
     */
    @Override
    public void sucessResponse(String data, int method) {}
    /**
     * 接口请求失败
     * @param error
     * @param method   判断是哪个接口返回的数据
     */
    @Override
    public void failedResponse(String error, int method) {}
    /**
     * 无网络
     * @param error
     * @param method   判断是哪个接口返回的数据
     */
    @Override
    public void networkResponse(String error, int method) {}
}
